package com.maijinjie.common.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 2013-12-13
 * @author 罗正杰
 * 用于POI Excel 生成工具
 */
public class PoiExcelUtil {

	
	/**
	 * header 标题，fields 返回结果集字段名称 ， list 结果集  ， 映射 Class 如果为Map 类型可以设置为Null
	 * @param header
	 * @param fields
	 * @param list
	 * @return
	 */
	public static Workbook createSimpleExportWorkbook(Object[] header,Object[] fields,List datalist,Class dataClass,String timeStr){		
		XSSFWorkbook wb = new XSSFWorkbook();
		if(header != null && fields != null && datalist != null){
			XSSFSheet sheet =  wb.createSheet("export");
			createHeader(sheet,header);
			createContent(sheet,fields,datalist,dataClass,timeStr);
		}
		
		return wb;
	}

	/**
	 * 
	 * @param fields 结果集类名集合
	 * @param datalist 结果集
	 * @param timeStr  时间字符串
	 * @param list
	 * @return
	 */
	private static void createContent(XSSFSheet sheet, Object[] fields,
			List datalist, Class dataClass, String timeStr) {
		XSSFRow row = null;
		XSSFCell cell = null;
		
		for(int i = 0 ; i <datalist.size() ; i++){
			row = sheet.createRow(i+1);
			Object obj = datalist.get(i);
			if(dataClass == null){
				Map map = (Map) obj;
				Object data = null;
				for(int j = 0 ; j < fields.length ; j++){
					cell = row.createCell(j);
					String field = fields[j].toString();
					setValue(cell,map.get(field),timeStr);
				}
			}else{
				if(fields == null){
					Field[] classField =  dataClass.getDeclaredFields();
					if(classField != null && classField.length>0){
						fields = new String[classField.length];
						for(int k=0 ; k< classField.length ; k++){
							fields[k] = classField[k].getName();
						}
					}
				}
				for(int j = 0 ; j < fields.length ; j++){
					cell = row.createCell(j);
					String field = fields[j].toString();
					String methodName = getTheGetMethodName(field);
					try {
						setValue(cell,dataClass.getMethod(methodName, null).invoke(obj, null),timeStr);
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					} catch (NoSuchMethodException e) {
						try {
							setValue(cell,dataClass.getMethod("get"+field, null).invoke(obj, null),timeStr);
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					} catch (SecurityException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}


	private static void setValue(XSSFCell cell, Object obj, String timeStr) {
		if(obj != null){
			if(obj instanceof String){
				cell.setCellValue((String) obj);
			}else if(obj instanceof Integer){
				cell.setCellValue(((Integer)obj).toString());
			}else if(obj instanceof Double){
				cell.setCellValue((Double)obj);
			}else if(obj instanceof Date){
				if(timeStr != null){
					SimpleDateFormat sf = new SimpleDateFormat(timeStr);
					cell.setCellValue(sf.format(obj));
				}else{
					cell.setCellValue((Date)obj);
				}
			}else {
				cell.setCellValue(obj.toString());
			}
		}else{
			cell.setCellValue("");
		}
	}

	private static String getTheGetMethodName(String field) {
		if(field.length() > 1){
			return "get"+field.substring(0,1).toUpperCase()+field.substring(1);
		}else{
			return "get"+field.toUpperCase();
		}
	}

	private static void createHeader(XSSFSheet sheet, Object[] header) {
		XSSFRow row = sheet.createRow(0);
		for(int i = 0 ; i< header.length ; i++){
			XSSFCell cell = row.createCell(i);
			cell.setCellValue(header[i].toString());
		}
	}

	
}
